package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.entity.Permission;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author 课工场
 * @date 2024/12/23
 * @description 权限控制
 */
@RestController
@RequestMapping("permission")
public class PermissionController {


    @Resource
    private PermissionService permissionService;

    // 权限列表
    @GetMapping("list")
    public ResponseResult permissionList() {

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        // 指定字段查询
        lambda.select(Permission::getId, Permission::getPid, Permission::getTitle);
        return ResponseResult.success().data("treeData", permissionService.list(lambda));
    }


    // 权限tree列表
    @GetMapping("tree")
    public ResponseResult permissionTree() {
        return ResponseResult.success().data("permissionTree", permissionService.list());
    }


    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdatePermission(@RequestBody Permission permission) {

        permissionService.saveOrUpdate(permission);
        return ResponseResult.success();
    }

    @GetMapping("permissionInfo/{id}")
    public ResponseResult selectPermissionInfo(@PathVariable Integer id) {
        Permission permission= permissionService.getById(id);
        Integer pid = permission.getPid();
        Permission p = permissionService.getById(pid);
        if (ObjectUtils.isNotNull(p)){
            return ResponseResult.success().data("permission",permission)
                    .data("title",p.getTitle());
        }
        return ResponseResult.success().data("permission",permission);
    }


    @DeleteMapping("/delete/{id}")
    public ResponseResult removePermissionById(@PathVariable Integer id){

        return  permissionService.deletePermission(id);
    }
}
